#!/usr/bin/python3
# -*- coding: utf-8 -*-
import pandas as pd
import numpy  as np

eps = 1e-8

def signal(*args):
    # Rsi_v2
    df = args[0]
    n = args[1]
    factor_name = args[2]

    close_dif = df['close'].diff()
    df['up'] = np.where(close_dif > 0, close_dif, 0)
    df['down'] = np.where(close_dif < 0, abs(close_dif), 0)
    a = df['up'].rolling(n).sum()
    b = df['down'].rolling(n).sum()
    df[factor_name] = a / (a + b + eps)

    # 删除多余列
    del df['up'], df['down']

    return df
